package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.*
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.android.synthetic.main.dialog_choose_layout.*
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.StringBuilder

class ChooseZhuanRuDialog(context: Context):BaseDialog(context) {
    override fun getLayoutRes() = R.layout.dialog_choose_layout

    private var itemDialog = ItemListDialog(context)
    private val locationArr = intArrayOf(-1,-1,-1,-1)
    private val mApi = RetrofitUtils.initRetrofit(ApiService::class.java)
    init {
        setCancelable(false)

        mbNegative.setOnClickListener { dismiss() }
    }

    fun show(onClick:(String,String,Boolean) -> Unit) {
        super.show()
        //选择设备
        tvIcebox.setOnClickListener {
            llProgress.visibility = View.VISIBLE
            iceboxData({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,bean.name))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    reset()
                    tvIcebox.text = name
                    locationArr[0] = id
                })
            })
        }

        //层数
        iceboxNumber.setOnClickListener {
            if(locationArr[0] == -1){
                ToastUtils.showShort("请先选择设备")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            requestIceboxNumberData({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,"${bean.row}"))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    locationArr[2] = -1
                    locationArr[3] = -1
                    tvDrawer.text = ""
                    tvLocation.text = ""

                    iceboxNumber.text = name
                    locationArr[1] = id
                })
            })
        }

        //抽屉
        tvDrawer.setOnClickListener {
            if(locationArr[1] == -1){
                ToastUtils.showShort("请先选择层数")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            iceboxDrawer({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,"${bean.location}"))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->

                    locationArr[3] = -1
                    tvLocation.text = ""

                    tvDrawer.text = name
                    locationArr[2] = id
                })
            })
        }

        //位置
        tvLocation.setOnClickListener {
            if(locationArr[2] == -1){
                ToastUtils.showShort("请先选择抽屉")
                return@setOnClickListener
            }
            llProgress.visibility = View.VISIBLE
            iceboxLocation({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,"${bean.location}"))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    tvLocation.text = "$name"
                    locationArr[3] = id
                })
            })
        }

        mbPositive.setOnClickListener {
            if(locationArr.find { it == -1 } != null){
                ToastUtils.showShort("请填写完整")
                return@setOnClickListener
            }
            onClick("${tvIcebox.text}\n${iceboxNumber.text}-${tvDrawer.text}-${tvLocation.text}","${locationArr[3]}",cbChoose.isChecked)
            dismiss()
        }
    }

    private fun reset() {
        locationArr[0] = -1
        locationArr[1] = -1
        locationArr[2] = -1
        locationArr[3] = -1
        iceboxNumber.text = ""
        tvDrawer.text = ""
        tvLocation.text = ""
    }

    //冰箱数据
    fun iceboxData(onResult:(MutableList<IceboxBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxData()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //层数
    fun requestIceboxNumberData(onResult:(MutableList<IceBoxNumberBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxNumberData("${locationArr[0]}")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //抽屉
    fun iceboxDrawer(onResult:(MutableList<IceboxDrawerBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxDrawer("${locationArr[1]}")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //位置
    fun iceboxLocation( onResult:(MutableList<IceboxLocationBean>)->Unit){
        MainScope().launch {
            val bean = mApi.iceboxLocation("${locationArr[2]}")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> {
                    llProgress.visibility = View.GONE
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }
}